// pages/location/location.js
let util = require("../../utils/util.js");
let baseImgUrl = util.baseImgUrl;
let app = getApp();
let moren_city="徐州";
Page({

  /**
   * 页面的初始数据
   */
  data: {
    networkStatus: true,//网络状态 默认为true
    loadingStatus: false,//默认加载完成，true--正在加载中
    loadgif: util.imgUrl + "loading.gif",//加载gif图
    loading: util.imgUrl + "loading.png",
    none_network: util.imgUrl + "none_network.png",

    position:baseImgUrl+"position_red.png",//定位图标
    enter:baseImgUrl+"enter.png",//进入图片
    point:util.imgUrl+"point.png",//点
    shut:baseImgUrl+"shut.png",//关闭按钮
    shutGrey: baseImgUrl +"shut_grey.png",//关闭按钮灰色
    city:"",
    district:"",
    cityId:2,
    districtId:0,//上一页页面选择的区域，
    
    districtMenuFlag:false,
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (option) {
    console.log("====================================location onload");
    var that = this;
    var quyuId=parseInt(option.quyuId);
   
    console.log(quyuId);
    that.setData({
      loadingStatus: true,
      districtId:quyuId,
      quyuId: quyuId
    })
    /********判断网络************ */
    wx.getNetworkType({
      success: function (res) {
        var networkType = res.networkType;
        if (networkType == "none" || networkType == "unknown") {
          that.setData({
            networkStatus: false
          })
        } else {
          that.setData({
            networkStatus: true,
          })
          wx.request({

            url: util.get_city,
            data: {
            },
            method: "POST",
            header: {
              "Content-Type": "application/x-www-form-urlencoded" //post
            },
            success: function (res) {
              console.log(res);
              console.log(typeof(res.data.districtList[0].id));
              that.setData({
                districtList: res.data.districtList,
                otherList: res.data.otherList,
                loadingStatus: false
              })
            }
          })
        }
      }, fail: function (err) {
        console.log("获取网络失败" + err);
        that.setData({
          networkStatus: false
        })
      }
    })
   

    /**************已进入页面，给前端上个页面的区域的id,前端根据这个id确定哪个区县是之前选中的，让其变色************** */
    
  },
  /**********点击顶部进入首页****************** */
  // 疑问 是否要带参数，带那些参数，城市？区县？
  enterBtn:function(){
    app.globalData.areaid = '';
     
      wx.navigateBack({
        delta:1
      })
  },
  /**********确认是否进入下一级************* */
  changeDistrict:function(e) {
    var that=this;
    var districtId=e.currentTarget.dataset.id;
    console.log(districtId);
    wx.showModal({
      title: '',
      content: '确认进入该区域',
      showCancel: true,
      cancelText: '取消',
      cancelColor: '#666',
      confirmText: '确认',
      confirmColor: '#d33a3d',
      success: function(res) {
        if (res.confirm) {
          app.globalData.areaid = districtId
          wx.switchTab({
            url: '/pages/index/index',
          })
        } else if (res.cancel) {
          console.log('用户点击取消')
        }
       
      },
      fail: function(res) {},
      complete: function(res) {},
    })
  },
  
  /************点击城市名称 弹出区域选择菜单*************** */
  changeCity:function(e){
      var that=this;
      var cityId=e.currentTarget.dataset.id;//获取点击的城市的id
      //将城市id传给后台，请求数据
      wx.request({
        url: util.get_area,
        data:{
          cityid: cityId,
        },
        success:function(res){
          that.setData({
            bottomList: res.data.bottomList,
          })
        },
        fail:function(){

        }
      })
      that.setData({
        districtMenuFlag:true
      })
  },

  /***************点击底部区县  进入首页*************************** */
  otherDistrict:function(e){
    var that = this;
    var districtId = e.currentTarget.dataset.id;//获取点击的城市的id
    app.globalData.areaid = districtId
    wx.switchTab({
      url: '/pages/index/index',
    })
  },

  /**************关闭弹框*************** */
  closeMenu:function(){
    var that = this;
    that.setData({
      districtMenuFlag: false
    })
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function (options) {
    var that=this;
    console.log(options);

    // that.setData({
    //  quyuId: quyuId
    // })
    /***********定位***************** */
    wx.getLocation({
      type: 'wgs84',
      success: function (res) {
        var latitude = res.latitude
        var longitude = res.longitude
        var speed = res.speed
        var accuracy = res.accuracy;
        //45f2637ce0ee391b9f552e20f1816e05
        that.loadCity(longitude, latitude)
      }, fail: function () {
        console.log("----------用户拒绝定位-------------");
      }
    })
  },

  loadCity: function (longitude, latitude) {
    var that = this;
    var city;
    var district;
    wx.request({
      url: 'https://restapi.amap.com/v3/geocode/regeo?key=45f2637ce0ee391b9f552e20f1816e05&location=' + longitude + ',' + latitude,
      data: {},
      header: {
        'Content-Type': 'application/json'
      },
      success: function (res) {
        console.log("定位返回信息");
        console.log(res);
        // success  
        city = res.data.regeocode.addressComponent.city;
        city = city.split("市")[0];//截取城市名

        district = res.data.regeocode.addressComponent.district;


        that.setData({
          city: city,
          district: district
        })
        if (city != moren_city) {
          page.setData({
            greyFlag: true
          })
        }
      },
      fail: function () {
        page.setData({
          currentCity: "获取定位失败",

        });

      },

    })
  },
  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  }
})